﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.MasterPage
{
    protected override void OnInit(EventArgs e)
    {
        dsFooterServices.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsFooterServices_SelectData);
        dsFooterSectors.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsFooterSectors_SelectData);
        dsFooterFooters.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsFooterFooters_SelectData);
        btnSearch.Click += new EventHandler(btnSearch_Click);
        base.OnInit(e);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (rptFooterServices.Items.Count == 0)
        {
            rptFooterServices.Visible = false;
        }
        if (rptFooterSectors.Items.Count == 0)
        {
            rptFooterSectors.Visible = false;
        }
        if (rptFooterFooters.Items.Count == 0)
        {
            rptFooterFooters.Visible = false;
        }
    }

    void dsFooterFooters_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        if (AcmsPage != null)
        {
            e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsPage.AcmsContext.SiteId, "~/Footers", AcmsPage.AcmsContext.SiteVersion));
        }
    }

    void dsFooterSectors_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        if (AcmsPage != null)
        {
            e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsPage.AcmsContext.SiteId, "~/Sectors", AcmsPage.AcmsContext.SiteVersion));
        }
    }

    void dsFooterServices_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        if (AcmsPage != null)
        {
            e.SetData(Post.GetAllBySiteIdMenuIdAndParentPath(AcmsPage.AcmsContext.SiteId, NavigationMenu.Id, "~/Services", AcmsPage.AcmsContext.SiteVersion));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public override void DataBind()
    {
        BindMenu();
        base.DataBind();
    }

    protected TemplatePage AcmsPage
    {
        get { return this.Page as TemplatePage; }
    }

    const string NavigationMenuName = "Navigation";

    private Acms.Bll.Menu _NavigationMenu;
    protected Acms.Bll.Menu NavigationMenu
    {
        get
        {
            if (_NavigationMenu == null)
            {
                _NavigationMenu = Acms.Bll.Menu.GetByNameAndSiteId(NavigationMenuName, AcmsPage.AcmsContext.SiteId);
                if (_NavigationMenu == null)
                {
                    throw new NullReferenceException(string.Format("Unable to load menu '{0}'", NavigationMenuName));
                }
            }
            return _NavigationMenu;
        }
    }

    private void BindMenu()
    {
        if(AcmsPage != null)
        {
            //Get HomePage
            Post home = Post.GetTopLevelBySiteId(AcmsPage.AcmsContext.SiteId, AcmsPage.AcmsContext.SiteVersion);
            if(home != null)
            {
                radPanel.Items.Add(BindMenuPost(AcmsPage.AcmsContext, home));
                if (AcmsPage.AcmsContext.CurrentPost != null && home.Id == AcmsPage.AcmsContext.CurrentPost.Id)
                {
                    radPanel.Items[0].Selected = true;
                }
                List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(AcmsPage.AcmsContext.SiteId, NavigationMenu.Id, home.PostGuid, AcmsPage.AcmsContext.SiteVersion);
                BindPostChildren(radPanel.Items, AcmsPage.AcmsContext, tmp, NavigationMenu);
            }
        }

    }

    private void BindPostChildren(RadPanelItemCollection items, AcmsContext acmsContext, List<Post> children, Acms.Bll.Menu menu)
    {
        foreach (Post child in children)
        {
            RadPanelItem item = BindMenuPost(acmsContext, child);
            items.Add(item);
            bool expanded = false;
            if (acmsContext.CurrentPost != null && acmsContext.CurrentPost.Id == child.Id)
            {
                //not sure why this is needed as i thought seting the item to selected should have set the class...
                item.CssClass = string.Concat(item.CssClass, " rpSelected");
                item.Selected = true;
                expanded = true;
            }
            else if (acmsContext.CurrentPost != null && acmsContext.CurrentPost.Path.StartsWith(child.Path) && child.Path != "~")
            {
                expanded = true;
            }

            if (expanded)
            {
                List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(AcmsPage.AcmsContext.SiteId, menu.Id, child.PostGuid, acmsContext.SiteVersion);
                if (tmp != null && tmp.Count > 0)
                {
                    BindPostChildren(item.Items, acmsContext, tmp, menu);
                }
                else
                {
                    item.Selected = true;
                }
            }
        }
    }

    private RadPanelItem BindMenuPost(AcmsContext acmsContext, Post post)
    {
        RadPanelItem tmp = new RadPanelItem(post.DisplayName, post.Url);
        string[] paths = post.Path.Split('/');
        if (paths.Length == 3)
        {
            tmp.ChildGroupCssClass = tmp.CssClass = string.Concat(paths[1].ToLower(), paths[2]).Replace(" ", "");
        }
        return tmp;
    }

    void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if(AcmsPage != null)
            {
                System.Text.StringBuilder url = new System.Text.StringBuilder();
                url.Append(Page.ResolveUrl(AcmsPage.AcmsContext.GetUrl("~/search.aspx")));
                if (url.ToString().Contains("?"))
                {
                    url.Append("&");
                }
                else
                {
                    url.Append("?");
                }
                url.AppendFormat("Term={0}", Server.UrlEncode(txtSearch.Text));
                Response.Redirect(url.ToString(), true);
                /*Post search = Post.GetBySiteIdAndPath(page.AcmsContext.SiteId, "~/Search", page.AcmsContext.SiteVersion);
                if(search != null)
                {
                    page.AcmsContext = new AcmsContext(search, "Default");
                    System.Text.StringBuilder url = new System.Text.StringBuilder( page.AcmsContext.TemplateUrl);
                    if (url.ToString().Contains("?"))
                    {
                        url.Append("&");
                    }
                    else
                    {
                        url.Append("?");
                    }
                    url.AppendFormat("Term={0}", Server.UrlEncode(txtSearch.Text));
                    Server.Transfer(url.ToString(), false);
                }*/
            }
        }
    }

}
